﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="VehicleTransactionRepository.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the  type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Data
{
    using System;
    using System.Data.Objects;
    using System.Diagnostics.Contracts;
    using System.Linq;
    using System.Linq.Expressions;
    using Entities;
    using Filters;
    using Interfaces;

    using Microsoft.Practices.ServiceLocation;

    /// <summary>
    /// Defines the <seealso cref="Hisaab.Data.VehicleTransactionRepository"/> type.
    /// </summary>
    public class VehicleTransactionRepository : IVehicleTransactionRepository
    {
        /// <summary>
        /// Databse instance to use
        /// </summary>
        private readonly IDatabase _database;

        /// <summary>
        /// Initializes a new instance of the <see cref="VehicleTransactionRepository"/> class.
        /// </summary>
        public VehicleTransactionRepository()
            : this(ServiceLocator.Current.GetInstance<IDatabase>())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="VehicleTransactionRepository"/> class.
        /// </summary>
        /// <param name="database">The database.</param>
        public VehicleTransactionRepository(IDatabase database)
        {
            Contract.Requires<ArgumentNullException>(database != null);
            _database = database;
        }

        /// <summary>
        /// Gets the filtered.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <param name="pageNo">The page no.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <returns>List of filtered items</returns>
        public IQueryable<VehicleTransaction> Query(VehicleTransaction filter, int? pageNo, int? pageSize)
        {
            return _database.VehicleTransactions
                .Filter(filter)
                .Page(pageNo, pageSize);
        }

        /// <summary>
        /// Queries the specified filter.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <param name="pageNo">The page no.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <returns>List of filtered items</returns>
        public IQueryable<VehicleTransaction> Filter(Expression<Func<VehicleTransaction, bool>> filter, int? pageNo, int? pageSize)
        {
            return _database.VehicleTransactions
                .Where(filter)
                .Page(pageNo, pageSize);
        }

        /// <summary>
        /// Finds the by id.
        /// </summary>
        /// <param name="id">The id of entity</param>
        /// <returns>Entity matching by id</returns>
        public VehicleTransaction FindById(int id)
        {
            return _database.VehicleTransactions
                .Filter(new VehicleTransaction { Id = id })
                .FirstOrDefault();
        }

        /// <summary>
        /// Adds the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns>Id of vehicle transaction added</returns>
        public int Add(VehicleTransaction entity)
        {
            _database.VehicleTransactions.AddObject(entity);
            _database.SaveChanges();
            _database.Refresh(RefreshMode.StoreWins, entity);
            return entity.Id;
        }

        /// <summary>
        /// Updates the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        public void Update(VehicleTransaction entity)
        {
            var previousEntity = _database.VehicleTransactions.Filter(new VehicleTransaction { Id = entity.Id }).FirstOrDefault();

            if (previousEntity != null)
            {
                previousEntity.Id = entity.Id;
                previousEntity.TransactionId = entity.TransactionId;
                previousEntity.Fill = entity.Fill;
                previousEntity.Milage = entity.Milage;
                previousEntity.Oil = entity.Oil;
                previousEntity.Service = entity.Service;
                previousEntity.Tires = entity.Tires;
                previousEntity.VehicleId = entity.VehicleId;
            }

            _database.SaveChanges();
        }

        /// <summary>
        /// Deletes the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        public void Delete(VehicleTransaction entity)
        {
            var previousEntity = _database.VehicleTransactions.Filter(new VehicleTransaction { Id = entity.Id }).FirstOrDefault();

            if (previousEntity != null)
            {
                _database.VehicleTransactions.DeleteObject(previousEntity);
            }

            _database.SaveChanges();
        }
    }
}